﻿using Se.Data.Sql;
using Se.Model.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Se.Model.Template.Common
{
    /// <summary>
    /// 配置管理器
    /// </summary>
    public class GameTemplateManager
    {
        public static GameTemplateManager Current = new GameTemplateManager();

        private DBManager m_DBManager;

        private IFileReader m_Reader;

        Dictionary<Type, List<DataEntityBase>> TemplateSet = new Dictionary<Type, List<DataEntityBase>>();

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="sqlType"></param>
        /// <param name="connectionString"></param>
        /// <param name="readerType"></param>
        public void Init(SqlType sqlType, string connectionString, ReaderType readerType, string templateFilePath)
        {
            switch (sqlType)
            {
                case SqlType.SQLServer:
                    this.m_DBManager = new DBManager(new SqlDataProvider(connectionString));
                    break;
                default:
                    throw new ArgumentException("SqlType not exist");
            }
            switch (readerType)
            {
                case ReaderType.CSV:
                    this.m_Reader = new CSVReader(templateFilePath);
                    break;
                default:
                    throw new ArgumentException("ReaderType not exist");
            }
        }

        /// <summary>
        /// 读取文件读取到DB
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="fileName"></param>
        public T[] ReadFileToDB<T>() where T : DataEntityBase
        {
            var entities = this.m_Reader.ReadFile<T>().ToArray();
            List<long> deletedIDs;
            this.m_DBManager.TryAddTable(typeof(T));
            this.m_DBManager.ClearDBEntity(typeof(T).Name);
            this.m_DBManager.SaveEntityCollection(typeof(T), out deletedIDs, entities);

            this.TemplateSet[typeof(T)] = DBEntityCommon.ConvertEntityBaseArray<T>(entities).ToList();
            return entities;
        }

        /// <summary>
        /// 加载游戏数据库
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public void LoadDBTemplate<T>() where T : DataEntityBase
        {
            var entities = this.m_DBManager.GetEntity<T>();

            this.TemplateSet[typeof(T)] = DBEntityCommon.ConvertEntityBaseArray<T>(entities).ToList();
        }

        /// <summary>
        /// 获取模板数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T[] GetTemplates<T>() where T : DataEntityBase
        {
            List<DataEntityBase> entities;
            if (this.TemplateSet.TryGetValue(typeof(T), out entities))
            {
                return DBEntityCommon.ConvertEntityArray<T>(entities.ToArray());
            }

            return null;
        }
    }
}
